import { Ref, ref, toRefs, watch } from "vue";
import { deepCopy } from "@core";
import { IContext, IParam } from "@/interface";

/**
 * @description 使用上下文参数逻辑块
 * @export
 * @param {Record<any, any>} props
 */
export function useContextParams(props: Record<any, any>) {
  const { contextProp, viewParamsProp } = toRefs(props);

  // 维护上下文并监控其变化
  const context: Ref<IContext> = ref({});
  watch(
    contextProp,
    (newVal: any, oldVal: any) => {
      context.value = deepCopy(newVal || {});
    },
    {
      immediate: true,
      deep: true,
    }
  );

  // 维护视图参数并监控其变化
  const viewParams: Ref<IParam> = ref({});
  watch(
    viewParamsProp,
    (newVal: any, oldVal: any) => {
      viewParams.value = deepCopy(newVal || {});
    },
    {
      immediate: true,
      deep: true,
    }
  );

  return { context, viewParams };
}
